package com.sixth.system.service.impl;/**
 * @author 633yh
 * @version 1.0
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sixth.core.entity.Notice;
import com.sixth.core.entity.User;
import com.sixth.core.vo.R;
import com.sixth.system.mapper.NoticeMapper;
import com.sixth.system.mapper.UserMapper;
import com.sixth.system.service.NoticeService;
import com.sixth.system.service.UserService;
import com.sixth.system.vo.NoticeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

import static com.sixth.core.util.WebUtil.getToken;
import static com.sixth.core.util.WebUtil.getUsername;

/**
 * @author     ：Stu YH
 * @date       ：Created in 2024/8/18 18:30
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class NoticeServiceimpl implements NoticeService {
   @Autowired
   private NoticeMapper noticeMapper;
   @Autowired
   private UserService userService;

   @Override
   public R<Page<Notice>> selectByPage(NoticeVo noticeVo) {
      Page<Notice> page = new Page<>(noticeVo.getPageNum(), noticeVo.getPageSize());
      QueryWrapper<Notice> wrapper = new QueryWrapper<>();
      if (noticeVo.getNoticeTitle() != null) {
         wrapper.like("notice_title", noticeVo.getNoticeTitle());
      }
      if (noticeVo.getNoticeType() != null) {
         wrapper.eq("notice_type", noticeVo.getNoticeType());
      }
      if (noticeVo.getStatus()!=null){
         wrapper.eq("status", noticeVo.getStatus());
      }
      if (noticeVo.getCreateBy()!=null){
         wrapper.like("create_by", noticeVo.getCreateBy());
      }
      Page<Notice> noticeIPage = noticeMapper.selectPage(page, wrapper);
      return R.ok(noticeIPage);
   }

   @Override
   public boolean addUpdate(Notice notice) {
      notice.setCreateTime(new java.util.Date());
      // 获取当前登录用户名
      String usertelephone = getUsername();
      R<User> userR = userService.selectBytel(usertelephone);
      System.out.println("userR " + userR);
      notice.setCreateBy(userR.getData().getUserName());
      noticeMapper.insertOrUpdate(notice);
      return true;
   }

   @Override
   public R delete(Long[] noticeIds) {
      noticeMapper.deleteBatchIds(Arrays.asList(noticeIds));
      return R.ok();
   }

   @Override
   public R<Notice> getById(Long noticeId) {
      return R.ok(noticeMapper.selectById(noticeId));
   }

   @Override
   public R<List<Notice>> listAllNotice() {
      return R.ok(noticeMapper.selectList(null));
   }
}